﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title></title>
	<link href="js-console.css" rel="stylesheet" />
	<!-- Write a script that enters the coefficients a, b and c of a quadratic equation
		a*x2 + b*x + c = 0
		and calculates and prints its real roots. Note that quadratic equations
		may have 0, 1 or 2 real roots. -->
</head>
<body>
	<div id="js-console"></div>
	<script src="js-console.js"></script>
	<script>
		//Read input
		var a = prompt("Enter coefficient A");
		var b = prompt("Enter coefficient B");
		var c = prompt("Enter coefficient C");

		if (!isNaN(a) && !isNaN(b) && !isNaN(c)) {
			a *= 1; b *= 1; c *= 1;	// make them real number

			// Write the input
			jsConsole.writeLine("Equation: " + a + "*x^2 + " + b + "*x + " + c + " = 0");

			// If it is not quadratic equation === A is zero
			if (a == 0) {
				jsConsole.writeLine("<strong>It is not quadratic equation - coefficient A is Zero</strong>");
				jsConsole.writeLine("One real root: " + (-c / b));
			}
			else {
				var d = (b * b) - (4.0 * a * c);

				if (d >= 0.0) {
					if (d == 0.0)
					{ jsConsole.writeLine("One real root x = " + ((-1.0 * b) / (2 * a))); }
					else
					{
						var x1 = ((-1.0 * b) + Math.sqrt(d)) / (2 * a);
						var x2 = ((-1.0 * b) - Math.sqrt(d)) / (2 * a);
						jsConsole.writeLine("Two real roots: x1 = " + x1 + " and x2 = " + x2);
					}
				}
				else {
					jsConsole.writeLine("Zero real roots.");
				}
			}

			// Write result
			jsConsole.writeLine();
		}
		else {
			jsConsole.writeLine("Wrong input.");
		}
	</script>
</body>
</html>
